//
//  AppDelegate.swift
//  Weibo
//
//  Created by 李学林 on 15/12/4.
//  Copyright © 2015年 tete. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var observer: NSObjectProtocol?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        window?.backgroundColor = UIColor .whiteColor()
        
        window?.rootViewController = HMWBUserInfoViewModel.sharedAccount.userLogin ? defatulRootViewController() : HMTabBarViewController()
        
        window?.makeKeyAndVisible()
        
        observer = NSNotificationCenter.defaultCenter().addObserverForName(HMSwitchRootVCNotifation, object: nil, queue: nil) {[weak self] (noti) -> Void in
            print("切换根控制器")
            if noti.object is HMOAuthViewController {
                self?.window?.rootViewController = HMWelcomeViewController()
            }else{
                self?.window?.rootViewController = HMTabBarViewController()
            }
        }
        return true
        }
    
    
    private func defatulRootViewController() ->UIViewController{
    
        if hasNewVersion(){
            return HMNewFeatureViewController()
        }else{
            return HMWelcomeViewController()
        }
    
    }
    
    private func hasNewVersion() -> Bool {
        
        let versionKey = "versionKey"
        let localVersion = NSUserDefaults.standardUserDefaults().stringForKey(versionKey)
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"]!as!String
        NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: versionKey)
        
        if let local = localVersion where
            currentVersion.compare(local) != NSComparisonResult.OrderedDescending{
            return false
        }
        // 进行对比
        return true
    }




}

